package com.xiaomengkuan.common.util;


import com.xiaomengkuan.common.entity.vo.PageVo;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

/**
 * @author Faraj Mujey
 * -- Creation Time: 2021-01-09 8:34 下午, File Encoding: utf8 --
 */
public class YsjUtil {
    public static String getTimeStr(Date date){
        long timestamp = date.getTime();
        return getTimeStr(timestamp);
    }
    public static String getTimeStr(LocalDateTime date){
        long tempTimestamp = date.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return getTimeStr(tempTimestamp);
    }
    private static String getTimeStr(long timestamp){
        long nowTimestamp = new Date().getTime();
        long difference = nowTimestamp - timestamp;
        if (difference < 60000){
            return "刚刚";
        } else if(difference < 120000) {
            return "两分钟前";
        } else if (difference < 180000) {
            return "三分钟前";
        } else if (difference < 240000) {
            return "四分钟前";
        } else if (difference < 300000) {
            return "五分钟前";
        } else if (difference < 600000){
            return "十分钟前";
        } else if (difference < 900000){
            return "十五分钟前";
        } else if(difference < 1800000){
            return "半小时前";
        } else if(difference < 2400000){
            return "四十分钟前";
        } else if (difference < 3000000){
            return "五十分钟前";
        } else if(difference < 3600000) {
            return "一小时前";
        } else if(difference < 7200000) {
            return "两小时前";
        } else if(difference < 10800000) {
            return "三小时前";
        } else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM" +
                    "月dd日 HH:mm");
            return simpleDateFormat.format(timestamp);
        }
    }

    public static <T> PageVo<T> pageFilling(int current, int size, long total
            , List<T> records){
        PageVo<T> tPageVo = new PageVo<>();
        tPageVo.setCurrent(current);
        tPageVo.setSize(size);
        tPageVo.setTotal(total);
        tPageVo.setRecords(records);
        return tPageVo;
    }

    public static <T> PageVo<T> pageFilling(long current, long size,
                                            long total, List<T> records) {
        return pageFilling((int) current, (int) size, total, records);
    }

    public static boolean stringsCompliant(String... strings) {
        boolean flag = true;
        for (int i = 0; i < strings.length; i++) {
            if (StringUtils.isEmpty(strings[i])) {
                flag = false;
            }
        }
        return flag;
    }

    public static boolean objsCompliant(Object... objs) {
        boolean flag = true;
        for (int i = 0; i < objs.length; i++) {
            if (objs[i] == null) {
                flag = false;
            }
        }
        return flag;
    }
}
